package com.device.operating.controller;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.device.operating.entity.DataDictionary;
import com.device.operating.entity.Func;
import com.device.operating.entity.Functype;
import com.device.operating.entity.Ugfunc;
import com.device.operating.entity.Ugfunctype;
import com.device.operating.service.DataDictionaryService;
import com.device.operating.service.DepartmentService;
import com.device.operating.service.DproleService;
import com.device.operating.service.FuncService;
import com.device.operating.service.FunctypeService;
import com.device.operating.service.GroupService;
import com.device.operating.service.RoleService;
import com.device.operating.service.UgService;
import com.device.operating.service.UgfuncService;
import com.device.operating.service.UgfunctypeService;
import com.device.operating.service.UserService;
import com.device.operating.utils.KeyProvider;


@Controller
public class FuncController extends BaseController{

	@Resource(name="userService")
	private UserService userService;
	
	@Resource(name="DepartmentService")
	private DepartmentService DepartmentService;
	
	@Resource(name="DataDictionaryService")
	private DataDictionaryService DataDictionaryService;
	
	@Resource(name="UgfuncService")
	private UgfuncService UgfuncService;
	
	@Resource(name="UgfunctypeService")
	private UgfunctypeService UgfunctypeService;
	
	@Resource(name="RoleService")
	private RoleService RoleService;
	
	@Resource(name="FunctypeService")
	private FunctypeService FunctypeService;
	
	@Resource(name="FuncService")
	private FuncService FuncService;
	
	@Resource(name="DproleService")
	private DproleService DproleService;
	
	@Resource(name="GroupService")
	private GroupService GroupService;
	
	@Resource(name="UgService")
	private UgService UgService;
	
	@RequestMapping("/tofunclist")
	public String tofunclist(HttpServletRequest request){
		return "func/funclist.jsp";
	}
	
	@RequestMapping("/getrootfunctype")
	@ResponseBody
	public List<JSONObject> getrootfunctype(){
		JSONObject jo=new JSONObject();
		List<JSONObject> listdt=new ArrayList<JSONObject>();
		List<JSONObject> listjson=new ArrayList<JSONObject>();
		Functype ft=FunctypeService.queryone("root");
		List<Functype> ld1=FunctypeService.querybyparentId("root");
		for(Functype depart:ld1){
			JSONObject json=new JSONObject();
			json.put("id", depart.getFunctypeId());
			json.put("text", depart.getFunctypename());
			
			if(FunctypeService.querybyparentId(depart.getFunctypeId()).size()>0){
				List<JSONObject> lj=new ArrayList<JSONObject>();
				json.put("state", "closed");
				json.put("children", lj);
				//dptree.setChildren(null);
			}
			listdt.add(json);
		}
		jo.put("id", "root");
		jo.put("functypesort", "0");
		jo.put("state", "closed");
		jo.put("text", ft.getFunctypename());
		jo.put("children", listdt);
		listjson.add(jo);
		return listjson;
	}
	
	@RequestMapping("/getallfunct")
	@ResponseBody
	public List<JSONObject> getallfunct(){
		List<Functype> listf=FunctypeService.querybyparentId("root");
		List<JSONObject> list=creatc(listf, "root");
		JSONObject json=new JSONObject();
		json.put("id", "root");
		json.put("text", "系统功能");
		json.put("state", "open");
		json.put("children", list);
		List<JSONObject> lj=new ArrayList<>();
		lj.add(json);
		return lj;
	}
	
	public List<JSONObject> creatc(List<Functype> listf,String functypeId){
		List<JSONObject> list=new ArrayList<>();
		List<Functype> listft=FunctypeService.querybyparentId(functypeId);
		for(Functype ft:listft){
			JSONObject json=new JSONObject();
			json.put("id", ft.getFunctypeId());
			json.put("text", ft.getFunctypename());
			json.put("functypesort", ft.getFunctypesort());
			json.put("parentId",ft.getParentId());
			json.put("state","open");
			json.put("children", creatc(listft, ft.getFunctypeId()));
			list.add(json);
		}
		return list;
	}
	
	
	@RequestMapping("/extendfunctypenode")
	@ResponseBody
	public List<JSONObject> extendfunctypenode(HttpServletRequest request){
		List<JSONObject> listdt=new ArrayList<JSONObject>();
		String dfId=request.getParameter("nodeId");
		List<Functype> ld1=FunctypeService.querybyparentId(dfId);
		for(Functype depart:ld1){
			JSONObject json=new JSONObject();
			json.put("functypesort", depart.getFunctypesort());
			json.put("id", depart.getFunctypeId());
			json.put("text", depart.getFunctypename());
			if(FunctypeService.querybyparentId(depart.getFunctypeId()).size()>0){
				List<JSONObject> lj=new ArrayList<JSONObject>();
				json.put("state", "closed");
				json.put("children", lj);
				//dptree.setChildren(null);
			}
			listdt.add(json);
		}		
		return listdt;
	}
	
	@RequestMapping("/getfunclist")
	@ResponseBody
	public Map getfunclist(HttpServletRequest request,
			@RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer rows){
		String functypeId=request.getParameter("functypeId");
		String ugId=request.getParameter("ugId");
		if((functypeId==null||functypeId.equals("")||functypeId.equals("0"))&&(ugId==null||ugId.equals(""))){
			return null;
		}
		return FuncService.queryallfuncbytypeId(functypeId,ugId, page-1, rows);
	}
	
	@RequestMapping("/updatefunclist")
	@ResponseBody
	public Map updatefunclist(HttpServletRequest request){
		Map map=new HashMap<>();
		String listu=request.getParameter("listupdate");
		String listd=request.getParameter("listdelete");
		String listn=request.getParameter("listnew");
		List<Func> listnew=null;
		if(!listn.equals("")){
			JSONArray ln=JSONArray.fromObject(listn);
			listnew=(List<Func>)JSONArray.toList(ln, Func.class);
		}		
		List<Func> listupdate=null;
		if(!listu.equals("")){
			JSONArray ln=JSONArray.fromObject(listu);
			listupdate=(List<Func>)JSONArray.toList(ln, Func.class);
		}
		
		List<Func> listdelete=null;
		if(!listd.equals("")){
			JSONArray ln=JSONArray.fromObject(listd);
			listdelete=(List<Func>)JSONArray.toList(ln, Func.class);
		}
		if(listnew!=null){
			for(Func func:listnew){
				func.setFuncId(KeyProvider.getPrimaryKey());
				FuncService.addfunc(func);
			}
		}
		if(listupdate!=null){
			for(Func func:listupdate){
				FuncService.addfunc(func);
			}
		}
		if(listdelete!=null){
			for(Func func:listdelete){
				FuncService.deletefunc(func.getFuncId());
			}
		}
		map.put("msg", "修改成功");
		return map;
	}
	
	@RequestMapping("/toaddnewfunctype")
	public String toaddnewfunctype(HttpServletRequest request,ModelMap map){
		String parentId=request.getParameter("parentId");
		String parentname=FunctypeService.queryone(parentId).getFunctypename();
		map.put("parentId", parentId);
		map.put("parentname", parentname);
		return "func/addfunctype.jsp";
	}
	
	@RequestMapping("/geticoncls")
	@ResponseBody
	public List<DataDictionary> geticoncls(HttpServletRequest request){
		String fieldId=request.getParameter("fieldId");
		List<DataDictionary> list=DataDictionaryService.querybyDataType(fieldId);
		return list;
	}
	
	@RequestMapping("/addfunctype")
	@ResponseBody
	public Map addfunctype(HttpServletRequest request){
		Map map=new HashMap<>();
		String parentId=request.getParameter("parentId");
		String parentname=request.getParameter("parentname");
		String functypename=request.getParameter("functypename");
		String fsort=request.getParameter("functypesort");
		String functypesort="";
		if(fsort.equals("jsp")){
			functypesort="1";
		}else
			functypesort="0";
		String functypedescribe=request.getParameter("functypedescribe");
		String iconcls=request.getParameter("iconcls");
		String functypecode=request.getParameter("functypecode");
		int functypenum=Integer.valueOf(request.getParameter("functypenum"));
		Functype ft=new Functype();
		ft.setFunctypecode(functypecode);
		ft.setFunctypedescribe(functypedescribe);
		ft.setFunctypeId(KeyProvider.getPrimaryKey());
		ft.setFunctypename(functypename);
		ft.setFunctypenum(functypenum);
		ft.setFunctypesort(functypesort);
		ft.setIconcls(iconcls);
		ft.setParentId(parentId);
		ft.setParentname(parentname);
		FunctypeService.addfunctype(ft);
		map.put("msg", "添加成功");
		return map;
	}
	
	@RequestMapping(value="deletefunctype",method=RequestMethod.POST)
	@ResponseBody
	public Map deletefunctype(HttpServletRequest request){
		Map map=new HashMap<>();
		String functypeId=request.getParameter("functypeId");
		Functype functype=FunctypeService.queryone(functypeId);
		String functypesort=functype.getFunctypesort();
		if(functypesort.equals("0")){//类型
			List<Functype> listfunctype=FunctypeService.querybyparentId(functypeId);
			if(listfunctype.size()!=0){
				map.put("msg", "有子功能项，无法删除!");
				return map;
			}
		}else{//JSP
			List<Func> listfunc=FuncService.querybyfunctypeId(functypeId);
			if(listfunc.size()!=0){
				map.put("msg", "有子功能项，请先删除子功能！");
				return map;
			}
		}
		FunctypeService.deletefunctype(functypeId);
		List<Ugfunctype> listuf=UgfunctypeService.querybyfunctypeId(functypeId);
		for(Ugfunctype uf:listuf){
			String ufId=uf.getUgfunctypeId();
			UgfunctypeService.deleteugfunctype(ufId);
		}	
		map.put("msg", "删除成功！");
		return map;
	}
	
	@RequestMapping(value="compleditfunctype",method=RequestMethod.POST)
	@ResponseBody
	public Map compleditfunctype(HttpServletRequest request){
		Map map=new HashMap<>();
		String functypeId=request.getParameter("functypeId");
		String functypename=request.getParameter("functypename");
		String functypenum=request.getParameter("functypenum");
		String iconcls=request.getParameter("iconcls");
		String functypecode=request.getParameter("functypecode");
		String functypedescribe=request.getParameter("functypedescribe");
		Functype functype=FunctypeService.queryone(functypeId);
		if(functype==null){
			map.put("msg", "不存在!");
			return map;
		}
		try{
			functype.setFunctypenum(Integer.valueOf(functypenum));
		}catch(Exception e){
			map.put("msg", "序号出错！");
			return map;
		}
		functype.setFunctypedescribe(functypedescribe);
		functype.setFunctypecode(functypecode);
		functype.setFunctypename(functypename);
		functype.setIconcls(iconcls);
		FunctypeService.addfunctype(functype);
		List<Ugfunctype> listuf=UgfunctypeService.querybyfunctypeId(functypeId);
		for(Ugfunctype uf:listuf){
			uf.setFunctypecode(functypecode);
			uf.setFunctypename(functypename);
			uf.setIconcls(iconcls);
			uf.setTypenum(Integer.valueOf(functypenum));
			UgfunctypeService.addugfunctype(uf);
		}
		map.put("msg", "编辑成功!");
		return map;
	}
	
	@RequestMapping("/toeditfunctype")
	public String toeditfunctype(HttpServletRequest request,ModelMap map){
		String functypeId=request.getParameter("functypeId");
		List<DataDictionary> listdata=DataDictionaryService.querybyDataType("iconcls");
		Functype functype=FunctypeService.queryone(functypeId);
		map.put("functype", functype);
		map.put("listdata", listdata);
		return "func/editfunctype.jsp";
	}
	
	@RequestMapping("getugfunctypebygpId")
	@ResponseBody
	public List<Ugfunctype> getugfunctypebygpId(HttpServletRequest request){		
		String gpId=request.getParameter("gpId");
		List<Ugfunctype> list=UgfunctypeService.querybyugIdandugtype(gpId, "0");//0代表组，1代表人员
		return list;
	}
	
	@RequestMapping("getugfuncbygpId")
	@ResponseBody
	public List<Ugfunc> getugfuncbygpId(HttpServletRequest request){
		String ugId=request.getParameter("ugId");
		List<Ugfunc> list=UgfuncService.querybyugIdandugtype(ugId, "0");
		return list;
	}
	
	@RequestMapping(value="editfunctypetogroup",method=RequestMethod.POST)
	@ResponseBody
	public Map editfunctypetogroup(HttpServletRequest request){
		Map map=new HashMap<>();
		String gpId=request.getParameter("gpId");
		String newnode=request.getParameter("newnodes");
		String deletenode=request.getParameter("deletenodes");
		if(!deletenode.equals("")){
			String[] deletenodes=deletenode.split(",");
			for(int i=0;i<deletenodes.length;i++){
				Ugfunctype uf=UgfunctypeService.querybyugIdandugtypeandfunctypeId(deletenodes[i], gpId, "0");
				if(uf!=null){
					UgfunctypeService.deleteugfunctype(uf.getUgfunctypeId());
				}
			}
		}
		if(!newnode.equals("")){
			String[] newnodes=newnode.split(",");
			for(int i=0;i<newnodes.length;i++){
				Ugfunctype uf=UgfunctypeService.querybyugIdandugtypeandfunctypeId(newnodes[i], gpId, "0");
				if(uf==null){
					Functype ft=FunctypeService.queryone(newnodes[i]);
//					if(ft.getFunctypesort().equals("0")){
//						continue;
//					}
					if(ft.getFunctypeId().equals("root")){
						continue;
					}
					uf=new Ugfunctype();
					uf.setFunctypeId(newnodes[i]);
					uf.setFunctypecode(ft.getFunctypecode());
					uf.setFunctypename(ft.getFunctypename());
					uf.setFunctypesort(ft.getFunctypesort());
					uf.setParentId(ft.getParentId());
					uf.setParentname(ft.getParentname());
					uf.setUgfunctypeId(KeyProvider.getPrimaryKey());
					uf.setUgId(gpId);
					uf.setTypenum(ft.getFunctypenum());
					uf.setIconcls(ft.getIconcls());
					uf.setUgtype("0");
					UgfunctypeService.addugfunctype(uf);
				}
			}			
		}
		map.put("msg", "操作成功!");
		return map;
	}
	
	@RequestMapping(value="editfunctogroup",method=RequestMethod.POST)
	@ResponseBody
	public Map editfunctogroup(HttpServletRequest request){
		Map map=new HashMap<>();
		String gpId=request.getParameter("gpId");
		String newnode=request.getParameter("newnodes");
		String deletenode=request.getParameter("deletenodes");
		if(!deletenode.equals("")){
			String[] deletenodes=deletenode.split(",");
			for(int i=0;i<deletenodes.length;i++){
				Ugfunc uf=UgfuncService.querybyugIdandugtypeandfuncId(deletenodes[i], gpId, "0");
				if(uf!=null){
					UgfuncService.deleteugfunc(uf.getUgfuncId());
				}
			}
		}
		if(!newnode.equals("")){
			String[] newnodes=newnode.split(",");
			for(int i=0;i<newnodes.length;i++){
				Ugfunc uf=UgfuncService.querybyugIdandugtypeandfuncId(newnodes[i], gpId, "0");
				if(uf==null){
					Func ft=FuncService.queryone(newnodes[i]);
					uf=new Ugfunc();
					uf.setFunccode(ft.getFunccode());
					uf.setFuncId(newnodes[i]);
					uf.setFuncname(ft.getFuncname());
					uf.setFuncsort(ft.getFuncsort());
					uf.setUgfuncId(KeyProvider.getPrimaryKey());
					uf.setUgId(gpId);
					uf.setUgtype("0");//代表组
					UgfuncService.addugfunc(uf);
				}
			}
		}
		map.put("msg", "操作成功");
		return map;
	}
}
